<?php 
/****
布尔教育 高端PHP培训
培  训: http://www.itbool.com
论  坛: http://www.zixue.it
****/

/*
//1.普通类的实例化，得到两个对象
class Single{
	public $rand;
	public function __construct(){
		$this->rand = mt_rand(100000,9999999999);
	}
}
var_dump(new Single());
var_dump(new Single());
*/

/*
//2.受保护的构造方法
class Single{
	public $rand;
	protected function __construct(){
		$this->rand = mt_rand(100000,9999999999);
	}

}
var_dump(new Single());
var_dump(new Single());
*/

/*
3.
class Single{
	public $rand;
	public function __construct(){
		$this->rand = mt_rand(100000,9999999999);
	}

	public function getins(){
		return new Single();
	}
}
//想用用getins就要实例化，只要实例化，就会调用构造方法，报错
var_dump(new Single());
var_dump(new Single());
*/

//4.转移控制权 设为静态
/*class Single{
	public $rand;
	public function __construct(){
		$this->rand = mt_rand(100000,9999999999);
	}

	static public function getins(){
		return new Single();
	}
}
var_dump(Single::getins());
var_dump(Single::getins());*/

//5.添加判断
class Single{
	public $rand;
	static public $ob = null;
	final function __construct(){
		$this->rand = mt_rand(100000,9999999999);
	}

	static public function getins(){
		if(Single::$ob === null) {
		 Single::$ob = new Single();
		}
		return Single::$ob;
	}
}

class Test extends Single{
	public function __construct(){

	}
}

var_dump(Single::getins());
var_dump(Single::getins());
?>